#include "uart.h"

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_uart.h"
#include "wifiiot_watchdog.h"



void uart_task(void *arg)
{
    (void) arg;
    uint8_t str[] = {"wite for send data\r\n"};
    uint8_t buff[20] = {0};
    uint8_t buff_lenth = 0;
    uart2_init();
    while(1)
    {
        UartWrite(WIFI_IOT_UART_IDX_2, str, sizeof(str));
        OLED_Show_String(0, 4, 16, str);
        buff_lenth = UartRead(WIFI_IOT_UART_IDX_2, buff, 20);
        if(buff_lenth > 0)
        {
            printf("buff_lenth = %d\r\n", buff_lenth);
            UartWrite(WIFI_IOT_UART_IDX_2, buff, buff_lenth);
            buff_lenth = 0;
        }
        printf("buff_lenth1 = %d\r\n", buff_lenth);
        usleep(500*1000);
    }
}

void uart_task_creat(void)
{
    osThreadAttr_t attr;

    attr.name       = "uart_task";
    attr.attr_bits  = 0U;
    attr.cb_mem     = NULL;
    attr.cb_size    = 0U;
    attr.stack_mem  = NULL;
    attr.stack_size = 10240;
    attr.priority   = osPriorityNormal2;

    if (osThreadNew(uart_task, NULL, &attr) == NULL)
    {
        printf("[uart_task_creat] Falied to create uart_task!\n");
    }
}

APP_FEATURE_INIT(uart_task_creat);
